package com.cgy.utils.base.maven;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;


/**
 * @author CHTK
 */
public class MavenHelper {

    private static final String LAST_UPDATED = ".lastUpdated";

    /**
     * @param path
     */
    public static void lastUpdate(String... path) {
        for (String filePath : path) {
            if (new File(filePath).exists()) {
                removeFile(filePath);
                placeHolder(filePath);
            }
        }
    }

    /**
     * @param path
     */
    @SuppressWarnings("serial")
    public static void removeFile(String path) {
        Collection<File> files = FileUtils.listFiles(new File(path), new FileFileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile() && (file.getName().endsWith(LAST_UPDATED) || file.getName().equals("m2e-lastUpdated.properties"));
            }
        }, new DirectoryFileFilter() {
        });
        for (File file : files) {
            try {
                System.out.println(file.getAbsolutePath());
                System.out.println("*************************************************");
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                continue;
            }
        }
    }


    /**
     * @param path
     */
    @SuppressWarnings("serial")
    public static void placeHolder(String path) {
        Collection<File> files = FileUtils.listFilesAndDirs(new File(path), new DirectoryFileFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.indexOf("${") > -1;
            }
        }, null);
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
        }
    }
    
    public static void main(String[] args) {
        MavenHelper.lastUpdate("H:\\maven\\repository1");
    }

}
